package com.ymatou.iapi.userregister.testcase;

import java.text.Normalizer.Form;
import java.util.Date;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.userregister.parameter.SendToEmailBean;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.SendToEmailCall;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.MailRecord;
import com.ymttest.database.model.ValidationCode;
import com.ymttest.database.model.YmtValidationCode;
import com.ymttest.database.sqlwapper.EmailWapper;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.YmtValidationCodeWapper;
import com.ymttest.database.sqlwapper.YmtValidationCodeWapperV2;
import com.ymttest.utils.TestDataManager;
/**
*重新发送邮件
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SendToEmail{
	private static SendToEmailBean sendtoemailBean;
	private static SendToEmailCall sendtoemailCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("重新发送邮件");
	}

	@Before
	public void caseUp() {
		sendtoemailBean=new SendToEmailBean();
		sendtoemailCall=new SendToEmailCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendtoemailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendToEmail_001() {
		Logger.start(true,"重新发送邮件成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			regbean.setLoginMail(TestDataManager.getEmail());
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			sendtoemailBean.setLId(userId);
			sendtoemailBean.setTomail(regbean.getLoginMail());
			sendtoemailBean.setUId(regbean.getLoginId());
			sendtoemailCall.setData(sendtoemailBean);
			sendtoemailCall.callService();	
			Logger.verifyEquals("true", sendtoemailCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", sendtoemailCall.getString("ResponseCode"), "验证ResponseCode");
			
			ValidationCode validationCode = new YmtValidationCodeWapperV2().getByActionUserAndValidateType(sendtoemailBean.getTomail(), null).get(0);
			Logger.verifyNotNull(validationCode.getCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(0, validationCode.getStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(0, validationCode.getResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SendToEmail_002() {
		Logger.start(true,"不输入lid,失败");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
//			sendtoemailBean.setLId(userId);
			sendtoemailBean.setTomail(regbean.getLoginMail());
			sendtoemailBean.setUId(regbean.getLoginId());
			sendtoemailCall.setData(sendtoemailBean);
			sendtoemailCall.callService();	
			Logger.verifyEquals("false", sendtoemailCall.getString("Success"), "验证Success");
			Logger.verifyEquals("400", sendtoemailCall.getString("ResponseCode"), "验证ResponseCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SendToEmail_003() {
		Logger.start(true,"不输入tomail，失败");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			sendtoemailBean.setLId(userId);
//			sendtoemailBean.setTomail(regbean.getLoginMail());
			sendtoemailBean.setUId(regbean.getLoginId());
			sendtoemailCall.setData(sendtoemailBean);
			sendtoemailCall.callService();	
			Logger.verifyEquals("false", sendtoemailCall.getString("Success"), "验证Success");
			Logger.verifyEquals("400", sendtoemailCall.getString("ResponseCode"), "验证ResponseCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SendToEmail_004() {
		Logger.start(true,"输入错误uid,失败");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			sendtoemailBean.setLId(-1);
			sendtoemailBean.setTomail(regbean.getLoginMail());
			sendtoemailBean.setUId(regbean.getLoginId());
			sendtoemailCall.setData(sendtoemailBean);
			sendtoemailCall.callService();	
			Logger.verifyEquals("false", sendtoemailCall.getString("Success"), "验证Success");
			Logger.verifyEquals("400", sendtoemailCall.getString("ResponseCode"), "验证ResponseCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendToEmail_005() {
		Logger.start(true,"重新发送邮件成功JIRACONN-12962验证邮件");
		try {
			long now=new Date().getTime();
			//考虑到服务器时差
			Thread.sleep(5000);
			String mail="zhouyi@ymatou.com";
			new JUserWapper().deleteUserinfoByEmail(mail);
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			regbean.setLoginMail(mail);
			regbean.setRegisterType(1);
			regbean.setRegSource(0);
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			sendtoemailBean.setLId(userId);
			sendtoemailBean.setTomail(regbean.getLoginMail());
			sendtoemailBean.setUId(regbean.getLoginId());
			sendtoemailCall.setData(sendtoemailBean);
			sendtoemailCall.callService();	
			Logger.verifyEquals("true", sendtoemailCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", sendtoemailCall.getString("ResponseCode"), "验证ResponseCode");
			
			YmtValidationCode validationCode = new YmtValidationCodeWapper().getByActionUserAndValidateType(sendtoemailBean.getTomail(), null).get(0);
			Logger.verifyNotNull(validationCode.getsValidateCode(), "验证数据库表Ymt_ValidationCode中字段sValidateCode");
			Logger.verifyEquals(0, validationCode.getiValidateStatus(), "验证数据库表Ymt_ValidationCode中字段Ivalidatestatus");
			Logger.verifyEquals(0, validationCode.getiValidateResult(), "验证数据库表Ymt_ValidationCode中字段iValidateResult");
			
			MailRecord mailinfo = new EmailWapper().selectMailRecordByToMail(mail).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mailinfo));
			Logger.verifyEquals(sendtoemailBean.getTomail(), mailinfo.getToMail(), "验证数据库表Mail_Record字段toMail");
			Logger.verifyEquals("BuyerRegistSuccess", mailinfo.getMailTemplateId(), "验证数据库表Mail_Record字段mailTemplateId");
			Logger.verifyIsContains(regbean.getLoginId(), mailinfo.getParameters(), "验证数据库表Mail_Record字段parameters包含loginid(是这个用户)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}